package com.intel.factorytest.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.intel.factorytest.R;
import android.os.SystemProperties;
//import android.telephony.MSimTelephonyManager;
//import com.codeaurora.telephony.msim.SubscriptionManager;
import android.util.Log;


public class SimcardTest extends BaseActivity {

    private static final String TAG = "SimcardTest";
    private final String KEY_STATUS = "item_test_status";

    private String oldstatus = null;
    private String testname = null;
    protected Button mSucBtn;

    private TextView mSimStatus;
    private TextView mSim2Status;
    private Timer mTimer = null;
    private static final int FRESH = 1;
    private TelephonyManager tm = null;
    private TelephonyManager sim2 = null;
    private String textString = "";

    private String confValue;
    private boolean bSIM2Test = false;

    private final static String TC_SIM2 ="SIM2";
    //add by wangsm
    private final static int SIM_1 = 0;
    private final static int SIM_2 = 1;
    private final static String PROP_MULTISIM = "persist.radio.multisim.config";
    //end by wangsm

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.simcard_layout);
        initView();

        mSucBtn = (Button) findViewById(R.id.success_button);
        mSucBtn.setVisibility(View.INVISIBLE);

        boolean sim1Exist = startTest(SIM_1);
        String sim1Str = null;
        if(sim1Exist){
            mSucBtn.setVisibility(View.VISIBLE);
            mSimStatus.setTextColor(Color.WHITE);
            sim1Str = "SIM1 : "+ getString(R.string.sim_ok);
        }else{
            mSimStatus.setTextColor(Color.RED);
            sim1Str = "SIM1 : "+ getString(R.string.sim_no);
        }
        mSimStatus.setText(sim1Str);

        boolean sim2Exist = startTest(SIM_2);
        String sim2Str = null;
        if(sim2Exist){
            mSucBtn.setVisibility(View.VISIBLE);
           mSim2Status.setTextColor(Color.WHITE);
           sim2Str = "SIM2 : "+ getString(R.string.sim_ok);
        }else{
            mSim2Status.setTextColor(Color.RED);
            sim2Str = "SIM2 : "+ getString(R.string.sim_no);
        }
        mSim2Status.setText(sim2Str);

        //remove by wangsm
        /*tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        int sim_state_int = tm.getSimState();

        String modemVersion = android.os.SystemProperties.get(
                "gsm.version.baseband", "");
        String imei = tm.getDeviceId();
        textString += "SIM1\nModem FW: " + modemVersion + "\nIMEI: " + imei;
        if (sim_state_int == TelephonyManager.SIM_STATE_READY) {
            textString += "\nSIM ISO: " + tm.getSimCountryIso()
                    + "\nSIM Operator:" + tm.getSimOperator()
                    + "\nSIM Operator Name:" + tm.getSimOperatorName()
                    + "\nSIM Serial Number:" + tm.getSimSerialNumber();
            mSucBtn.setVisibility(View.VISIBLE);
            mSimStatus.setTextColor(Color.WHITE);

        } else {
//            textString += "\nSIM ISO: " + "" + "\nSIM Operator:" + ""
//                    + "\nSIM Operator Name:" + "" + "\nSIM Serial Number:" + "";
            textString +="\n"+"SIM1"+getString(R.string.sim_no);
            mSimStatus.setTextColor(Color.RED);

        }
        mSimStatus.setText(textString);

        Intent intent = getIntent();
        confValue = intent.getStringExtra("value");
        initTestByConfig(confValue);

        Log.i("SimcardTest","Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >=20) {
            bSIM2Test =false;
        }
        //remove by wangsm for sim2
        if (!isSoFIAProduct())
        {
            bSIM2Test =false;
        }*/
        //end by wangsm
    }

    private void initTestByConfig(String config) {
        if (config == null || config.trim().length() == 0) return;

        Log.i("tangjun comprehensive test","config = " + config);

        String[] configs = config.split(";");
        for (String conf : configs) {
            String[] items = conf.split("=");

            // overall sensors' switch
            if (items.length == 1) {
                if (items[0].equals("0")) {
                    // Don't test any Sensor!
                    //bSIM2Test = false;
                    break;
                }
                continue;
            }


         if (items[0].equals(TC_SIM2)) {
            bSIM2Test = true;
        }

        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == FRESH) {

                boolean sim1Exist = startTest(SIM_1);
                String sim1Str = null;
                if(sim1Exist){
                    mSucBtn.setVisibility(View.VISIBLE);
                    mSimStatus.setTextColor(Color.WHITE);
                    sim1Str = "SIM1 : "+ getString(R.string.sim_ok);
                }else{
                    mSimStatus.setTextColor(Color.RED);
                    sim1Str = "SIM1 : "+ getString(R.string.sim_no);
                }
                mSimStatus.setText(sim1Str);

                boolean sim2Exist = startTest(SIM_2);
                String sim2Str = null;
                if(sim2Exist){
                    mSucBtn.setVisibility(View.VISIBLE);
                    mSim2Status.setTextColor(Color.WHITE);
                    sim2Str = "SIM2 : "+ getString(R.string.sim_ok);
                }else{
                    mSim2Status.setTextColor(Color.RED);
                    sim2Str = "SIM2 : "+ getString(R.string.sim_no);
                }
                mSim2Status.setText(sim2Str);

                //remove by wangsm
                /*
                android.util.Log.i("mei", "handleMessage");
                int sim_state_int = tm.getSimState();
                String modemVersion = android.os.SystemProperties.get(
                        "gsm.version.baseband", "");
                String imei = tm.getDeviceId();
                textString = "";
                textString += "SIM1\nModem FW: " + modemVersion + "\nIMEI: " + imei;
                if (sim_state_int == TelephonyManager.SIM_STATE_READY) {
                    textString += "\nSIM ISO: " + tm.getSimCountryIso()
                            + "\nSIM Operator:" + tm.getSimOperator()
                            + "\nSIM Operator Name:" + tm.getSimOperatorName()
                            + "\nSIM Serial Number:" + tm.getSimSerialNumber();
                    mSimStatus.setTextColor(Color.WHITE);

                } else {
//                    textString += "\nSIM ISO: " + "" + "\nSIM Operator:" + ""
//                            + "\nSIM Operator Name:" + ""
//                            + "\nSIM Serial Number:" + "";
                    textString +="\n"+"SIM1"+getString(R.string.sim_no);
                    mSimStatus.setTextColor(Color.RED);

                }
                mSimStatus.setText(textString);

                if (bSIM2Test && sim2 != null)
                {
                    int sim2_state_int = sim2.getSimState();
                    String imei2 = sim2.getDeviceId();
                    textString =  "\nSIM2\nIMEI2: " + imei2;
                    if (sim2_state_int == TelephonyManager.SIM_STATE_READY) {
                        textString += "\nSIM2 ISO: " + sim2.getSimCountryIso()
                                + "\nSIM2 Operator:" + sim2.getSimOperator()
                                + "\nSIM2 Operator Name:" + sim2.getSimOperatorName()
                                + "\nSIM2 Serial Number:" + sim2.getSimSerialNumber();
                        mSim2Status.setTextColor(Color.WHITE);

                    } else {
                        textString +="\n"+"SIM2"+getString(R.string.sim_no);
                        mSim2Status.setTextColor(Color.RED);

                    }
                    mSim2Status.setText(textString);
                }
            */
            //end by wangsm
                }

        }

    };

    protected void initView() {
        super.initView();
        mSimStatus = (TextView) findViewById(R.id.simcardinfoTextView);
        mSim2Status = (TextView) findViewById(R.id.simcard2infoTextView);
    }

    @Override
    protected void onDestroy() {
        if (mTimer != null) {
            mTimer.cancel();
        }
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                mHandler.sendEmptyMessage(FRESH);
            }

        }, 1000, 1000);
        mItemTestFinish = true;//add by wangsm
    }

    @Override
    public void finish() {
        tc.setTestResult(is_pass);
        tc.setResultInfo(textString);
        super.finish();
    }
    //add by wangsm

     private boolean startTest(int sim) {
            String IMSI = null;
            boolean result = false;
            String multiSim = SystemProperties.get(PROP_MULTISIM, "");
            Log.d(TAG, " simCard multiSim = " + multiSim);
            TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        
            IMSI = mTelephonyManager.getSubscriberId(sim);
            //Log.d(TAG, " startTest sim =  " + sim + " startTest IMSI = " + IMSI + " mTelephonyManager.getSimState(sim) = " + mTelephonyManager.getSimState(sim));
            if(mTelephonyManager.hasIccCard(sim)){
                Log.d(TAG, " startTest hasIccCard ");
                result = true;
            } else if (mTelephonyManager.getSimState(sim) > TelephonyManager.SIM_STATE_ABSENT) {
                Log.d(TAG, " startTest getSimState ");
                result = true;
            }
             return result;
        }
    //end by wangsm
}
